/**
 * Created by John on 2015-04-20.
 */
var mysql = require('mysql');
var DB_NAME = 'speed';

var pool  = mysql.createPool({
    host     : '115.29.194.90',
    database:'speed',
    user     : 'root',
    password : 'u,4WmkdnAfDM4/H'
});
function Post(post){
    this.name = post.name;
    this.title = post.title;
    this.post=post.post;
    this.time=post.time
};
module.exports = Post;

pool.getConnection(function(err, connection) {
    var useDbSql = "USE " + DB_NAME;
    connection.query(useDbSql, function (err) {
        if (err) {
            console.log("USE Error: " + err.message);
            return;
        }
        console.log('USE succeed');
    });
});

    //保存数据
// Post.prototype.save = function save(callback) {
//     var date=new Date();
//     //存储各种时间，方便以后扩展
//     var time={
//         date:date,
//         year:date.getFullYear(),
//         month:date.getFullYear()+'-'+(date.getMonth()+1),
//         day:date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate(),
//         minute:date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+" "+date.getHours()+":"+(date.getMinutes()<10?'0'+
//         date.getMinutes():date.getMinutes())
//     };
//     //要存入数据库的文档
//     var post={
//         name:this.name,
//         time:time,
//         title:this.title,
//         post:this.post
//     };


//     pool.getConnection(function(err, connection) {
//         /**
//          * 在这里的this指向发生了变化
//          * @type {string}
//          */
//         var insertUser_Sql = "INSERT INTO posts(name,time,title,post) VALUES(?,?,?,?)";

//         connection.query(insertUser_Sql, [post.name, post.time.minute,post.title,post.post], function (err,result) {
//             if (err) {
//                 console.log("insertPost_Sql Error: " + err.message);
//                 return;
//             }

//             connection.release();

//             console.log("invoked[save]");
//             callback(err,result);
//         });
//     });
// };

// //读取文章的相关信息

Post.prototype.getPost=function getPost(name,callback){
        pool.getConnection(function(err,connection) {

            if(name==null){
              var   getPost_Sql = "SELECT * FROM SpeedRead WHERE 1 = ?";
                name=1;
            }
            else {

                var getPost_Sql = "SELECT * FROM SpeedRead WHERE id = ?";
            }

            connection.query(getPost_Sql, [39], function (err, result) {
                if (err) {
                    console.log("getPost Error: " + err.message);
                    return;
                }

                connection.release();

                console.log("invoked[getPost]");
                callback(err, result);
            });
        });
};

// Post.prototype.getPostByNameDayTitle=function getPostByNameDayTitle(name,time,title,callback){
//     pool.getConnection(function(err,connection){

//             var getPostByNameDayTitle_Sql="select * from posts where name=? and time=? and title=?";
//             connection.query(getPostByNameDayTitle_Sql,[name,time,title],function(err,result){
//                 if(err){
//                     console.log("getPostByNameDayTitle Error:"+ err.message);
//                     return;
//                 }

//                 connection.release();

//                 console.log("invoked[getPostByNameDayTitle]");
//                 callback(err,result);
//             });
//         }
//     );
// };

// Post.prototype.updatePost=function updatePost(post,callback){
//     pool.getConnection(function(err,connnection){
//         var updatePost_Sql="update posts set post=? where name=? and title=? and time=?";
//         //这里要注意日期的处理
//         connnection.query(updatePost_Sql,[post.post,post.name,post.title,post.time],function(err,result){
//             if(err){
//                 console.log('updatePost Error:' + err.message);
//                 return;
//             }
//             connnection.release();

//             callback(err,result);

//         });
//     });
// };

// Post.prototype.deletePost= function deletePost  (post,callback){
//     pool.getConnection(function(err,connection){
//         var deletePost_Sql="delete from posts where name=? and  title=? and  time=?";
//         connection.query(deletePost_Sql,[post.name,post.title,post.time],function(err,result){
//             if(err){
//                 console.log('deletePost Error'+ err.message);
//                 return;
//             }
//             connection.release();
//             callback(err,result);
//         });
//     });
// };